home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 April: Mac OS SDK / Dev.CD Apr 96 SDK / Dev.CD Apr 96 SDK1.toast / Development Kits (Disc 1) / OpenDoc Development Framework / ODFDev / ODF / OS / FWGraphx / Sources / FWPalete.cpp < prev    next >
Encoding:
Text File  |  1995-11-08  |  6.5 KB  |  247 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                FWPalete.cpp
  4. //    Release Version:    $ 1.0d11 $
  5. //
  6. //    Copyright:    © 1995 by Apple Computer, Inc., all rights reserved.
  7. //
  8. //========================================================================================
  9.  
  10. #include "FWOS.hpp"
  11.  
  12. #ifndef FWPALETE_H
  13. #include "FWPalete.h"
  14. #endif
  15.  
  16. #ifndef FWMEMHLP_H
  17. #include "FWMemHlp.h"
  18. #endif
  19.  
  20. #ifndef FWMEMMGR_H
  21. #include "FWMemMgr.h"
  22. #endif
  23.  
  24. #if defined(FW_BUILD_MAC) && !defined(__QDOFFSCREEN__)
  25. #include <QDOffscreen.h>
  26. #endif
  27.  
  28. //========================================================================================
  29. //    Runtime Informations
  30. //========================================================================================
  31.  
  32. #if FW_LIB_EXPORT_PRAGMAS
  33. #pragma lib_export on
  34. #endif
  35.  
  36. #ifdef FW_BUILD_MAC    
  37. #pragma segment FWGraphix_Palette
  38. #endif
  39.  
  40. //========================================================================================
  41. //    Palette functions
  42. //========================================================================================
  43.  
  44. //----------------------------------------------------------------------------------------
  45. //    FW_CreatePalette
  46. //----------------------------------------------------------------------------------------
  47.  
  48. FW_FUNC_ATTR    FW_Palette    FW_CreatePalette(
  49.         short                 nSize,
  50.         const FW_CColor*    colors,
  51.         short                nColorCount)
  52. {
  53. #ifdef FW_BUILD_WIN
  54.     LOGPALETTE* pLogPal = (LOGPALETTE*) new char[sizeof(LOGPALETTE) + nColorCount * sizeof(PALETTEENTRY)];
  55.     pLogPal->palVersion = 0x300;
  56.     pLogPal->palNumEntries = nColorCount;
  57.     
  58.     for (short i = 0; i < nColorCount; ++ i)
  59.     {
  60.         RGBQUAD rgb = colors[i];
  61.         
  62.         pLogPal->palPalEntry[i].peBlue    = rgb.rgbBlue;
  63.         pLogPal->palPalEntry[i].peGreen    = rgb.rgbGreen;
  64.         pLogPal->palPalEntry[i].peRed    = rgb.rgbRed;
  65.         pLogPal->palPalEntry[i].peFlags    = 0;
  66.     }
  67.  
  68.     HPALETTE hPal = ::CreatePalette(pLogPal);
  69.  
  70.     delete[] (char*) pLogPal;
  71.     
  72.     return hPal;
  73. #endif
  74. #ifdef FW_BUILD_MAC
  75.     Size colorTableSize = sizeof(ColorTable) + sizeof(ColorSpec) * (nColorCount - 1);
  76.     FW_CAcquireTemporarySystemHandle colorTableMemory(colorTableSize);
  77.     CTabPtr colorTablePtr = (CTabPtr) colorTableMemory.GetPointer();
  78.     
  79.     colorTablePtr->ctSeed = 0;
  80.     colorTablePtr->ctFlags = 0;
  81.     colorTablePtr->ctSize = nColorCount - 1;
  82.     
  83.     for (short i = 0; i < nColorCount; ++ i)
  84.     {
  85.         colorTablePtr->ctTable[i].value    = i;
  86.         colorTablePtr->ctTable[i].rgb = colors[i];
  87.     }
  88.     
  89.     colorTableMemory.Orphan();
  90.     CTabHandle hCTab = (CTabHandle) colorTableMemory.GetPlatformHandle();
  91.     
  92.     ::CTabChanged(hCTab);
  93.     return hCTab;
  94. #endif
  95. }
  96.  
  97. //----------------------------------------------------------------------------------------
  98. //    FW_GetPaletteSize
  99. //----------------------------------------------------------------------------------------
  100.  
  101. FW_FUNC_ATTR    short        FW_GetPaletteSize(FW_Palette palette)
  102. {
  103.     FW_ASSERT(palette != NULL);
  104.  
  105. #ifdef FW_BUILD_WIN
  106.     WORD wCount;
  107.     ::GetObject(palette, sizeof(WORD), &wCount);
  108.     return wCount;
  109. #endif
  110. #ifdef FW_BUILD_MAC
  111.     return (*palette)->ctSize + 1;
  112. #endif
  113. }
  114.  
  115. //----------------------------------------------------------------------------------------
  116. //    FW_SetPaletteEntries
  117. //----------------------------------------------------------------------------------------
  118.  
  119. FW_FUNC_ATTR    void        FW_SetPaletteEntries(
  120.         FW_Palette    palette,
  121.         const FW_CColor* colors,
  122.         short        nStartIndex,
  123.         short        nColorCount)
  124. {
  125.     FW_ASSERT(palette != NULL);
  126.     FW_ASSERT(nStartIndex + nColorCount <= FW_GetPaletteSize(palette));
  127.  
  128. #ifdef FW_BUILD_WIN
  129.     PALETTEENTRY* pPalEntries = new PALETTEENTRY[nColorCount];
  130.     
  131.     for (short i = 0; i < nColorCount; ++i, ++colors)
  132.     {
  133.         RGBQUAD rgb = *colors;
  134.         
  135.         pPalEntries[i].peBlue    = rgb.rgbBlue;
  136.         pPalEntries[i].peGreen    = rgb.rgbGreen;
  137.         pPalEntries[i].peRed    = rgb.rgbRed;
  138.         pPalEntries[i].peFlags    = 0;
  139.     }
  140.     
  141.     ::SetPaletteEntries(palette, nStartIndex, nColorCount, pPalEntries);
  142.  
  143.     delete[] pPalEntries;
  144. #endif
  145. #ifdef FW_BUILD_MAC
  146.     FW_CAcquireLockedSystemHandle lock((FW_PlatformHandle)palette);
  147.     CTabPtr colorTablePtr = (CTabPtr) lock.GetPointer();
  148.  
  149.     for (short i = nStartIndex; i < nStartIndex + nColorCount; ++ i, ++ colors)
  150.     {
  151.         colorTablePtr->ctTable[i].value = i;
  152.         colorTablePtr->ctTable[i].rgb = *colors;
  153.     }
  154.  
  155.     ::CTabChanged(palette);
  156. #endif
  157. }
  158.  
  159. //----------------------------------------------------------------------------------------
  160. //    FW_GetPaletteEntries
  161. //----------------------------------------------------------------------------------------
  162.  
  163. FW_FUNC_ATTR    void        FW_GetPaletteEntries(
  164.                                 FW_Palette    palette,
  165.                                 FW_CColor*    colors,
  166.                                 short        nStartIndex,
  167.                                 short        nColorCount)
  168. {
  169.     FW_ASSERT(palette != NULL);
  170.     FW_ASSERT(nStartIndex + nColorCount <= FW_GetPaletteSize(palette));
  171.  
  172. #ifdef FW_BUILD_WIN
  173.     PALETTEENTRY* pPalEntries = new PALETTEENTRY[nColorCount];
  174.     
  175.     ::GetPaletteEntries(palette, nStartIndex, nColorCount, pPalEntries);
  176.  
  177.     for (short i = 0; i < nColorCount; ++i, ++colors)
  178.     {
  179.         RGBQUAD rgb = *colors;
  180.         
  181.         rgb.rgbBlue        = pPalEntries[i].peBlue;
  182.         rgb.rgbGreen    = pPalEntries[i].peGreen;
  183.         rgb.rgbRed        = pPalEntries[i].peRed;
  184.         
  185.         *colors = rgb;
  186.     }
  187.     
  188.     delete[] pPalEntries;
  189. #endif
  190. #ifdef FW_BUILD_MAC
  191.     FW_CAcquireLockedSystemHandle lock((FW_PlatformHandle)palette);
  192.     CTabPtr colorTablePtr = (CTabPtr) lock.GetPointer();
  193.  
  194.     for (short i = nStartIndex; i < nStartIndex + nColorCount; ++ i, ++ colors)
  195.     {
  196.         *colors = colorTablePtr->ctTable[i].rgb;
  197.     }
  198. #endif
  199. }
  200.  
  201. //----------------------------------------------------------------------------------------
  202. //    FW_CopyPalette
  203. //----------------------------------------------------------------------------------------
  204.  
  205. FW_FUNC_ATTR    FW_Palette        FW_CopyPalette(FW_Palette palette)
  206. {
  207.     FW_ASSERT(palette != NULL);
  208.  
  209. #ifdef FW_BUILD_WIN
  210.     WORD wCount;
  211.     ::GetObject(palette, sizeof(WORD), &wCount);
  212.     
  213.     LOGPALETTE* pLogPal = (LOGPALETTE*) new char[sizeof(LOGPALETTE) + wCount * sizeof(PALETTEENTRY)];
  214.     
  215.     pLogPal->palVersion = 0x300;
  216.     pLogPal->palNumEntries = wCount;
  217.     
  218.     ::GetPaletteEntries(palette, 0, wCount, pLogPal->palPalEntry);
  219.     HPALETTE hPalCopy = ::CreatePalette(pLogPal);
  220.     
  221.     delete[] (char*) pLogPal;
  222.  
  223.     return hPalCopy;
  224. #endif
  225. #ifdef FW_BUILD_MAC
  226.     FW_Palette palCopy = (FW_Palette)
  227.         FW_CMemoryManager::CopySystemHandle((FW_PlatformHandle)palette);
  228.     (*palCopy)->ctSeed = 0;
  229.     ::CTabChanged(palCopy);
  230.     return palCopy;
  231. #endif
  232. }
  233.  
  234. //----------------------------------------------------------------------------------------
  235. //    FW_DestroyPalete
  236. //----------------------------------------------------------------------------------------
  237.  
  238. FW_FUNC_ATTR    void        FW_DestroyPalete(FW_Palette palette)
  239. {
  240. #ifdef FW_BUILD_WIN
  241.     ::DeleteObject(palette);
  242. #endif
  243. #ifdef FW_BUILD_MAC
  244.     ::DisposeCTable(palette);
  245. #endif
  246. }
  247.